#coding=utf-8

'''
Created on 2011-9-22

@author: Administrator
'''
import sys
import datetime
import os
sys.path.append('/harvester/ripper/source/src/')

from ripper.core.Utils import getId
from ripper.handler.Models import Forum, Post, Image, PostContent, Torrent, Url,RawContent #@UnresolvedImport
from ripper.core.Storage import db


class __SyncObj(object):

    def __init__(self):
        '''
        Constructor
        '''
        self.syncID = getId() # 同步ID 本次同步
    
    def transfer(self):
        forums = Forum.select()
        datas = self.query_data()
        count = datas.count()
        cur = 1
        for data in datas:
            print 'transfering ', cur, '/', count
            ret = self.save_data(data, forums)
            
            if False == ret : # 未下载完成
                print 'skipped.'
                continue
            
            elif None == ret: # 作废
                data['syncFlag'] = '2'
                data['syncDate'] = datetime.datetime.utcnow()
                db.save_item_data('data_hhentai', data)
                print 'discarded.'
                
            else : # 已同步
                data = ret
                data['syncFlag'] = '1'
                data['syncDate'] = datetime.datetime.utcnow()
                data['syncID'] = self.syncID
                db.save_item_data('data_hhentai', data)
                cur = cur + 1
        
    def query_data(self):
        q = db.db['data_hhentai'].find \
            ({'$or' : [{'syncFlag':None}, {'syncFlag' : '2'}] })    
        return q
    
    def save_data(self, data, forums):
        
        forum = 79
        
        if  data.has_key('images'):
            
            if data['images'] == None:
                return False
            
            # 如果有图片未下完则不同步
            for i in range(0, len(data['images'])):
                 image = data['images'][i]
                 if image['length'] == 0:
                     return False
            
            
            hashedUrl = hash(data['gIndexUrl'] )    
            post = Post(forum=forum,pageUrl=data['gIndexUrl'],\
                    title=data['title'],titleSimple=data['title'],author=data['author'],postDate=data['pubDate'],urlHash=hashedUrl)
            
            
            print 'postContent...'
            PostContent(post=post, content='')
            print 'RawContent...'
            RawContent(post=post, content='')
            
            print 'Image...'    
            tImg = data['titleImage']
            Image(post=post,filename=tImg['file'],url=tImg['url'], fileLength=tImg['length'], downloaded=1)
            for img in data['images']:
                Image(post=post,filename=img['file'],url=img['url'], fileLength=img['length'], downloaded=1)
            
            print data['gIndexUrl']
            data['syncTarget'] = post.id
            return data
        
        return False
    
    
def transfer_data():
    obj = __SyncObj()
    obj.transfer()
    
if __name__ == '__main__':
    obj = __SyncObj()
#    obj.fix_data_if_img_not_existed()
    obj.transfer()
    